package com.nest.WorkNest.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.nest.WorkNest.R;

import java.util.ArrayList;
import java.util.HashMap;

public class WaitReceiveAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<HashMap<String, String>> datalist;

    public WaitReceiveAdapter(Context context, ArrayList<HashMap<String, String>> datalist) {
        this.context = context;
        this.datalist = datalist;
    }

    @Override
    public int getCount() {
        return datalist.size();
    }

    @Override
    public Object getItem(int position) {
        return datalist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private class ViewHolder {
        ImageView image;
        TextView title;
        TextView num;
        TextView price;
        TextView orderId;
        TextView time;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.items_listview_waitreceive, null);
            holder.image = (ImageView) convertView.findViewById(R.id.image);
            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.num = (TextView) convertView.findViewById(R.id.num);
            holder.price = (TextView) convertView.findViewById(R.id.price);
            holder.orderId = (TextView) convertView.findViewById(R.id.orderId);
            holder.time = (TextView) convertView.findViewById(R.id.time);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.title.setText(datalist.get(position).get("productName"));
        holder.time.setText("下单时间:" + datalist.get(position).get("orderTime"));
        holder.num.setText("总数量(件):" + datalist.get(position).get("totalQuantity"));
        holder.price.setText("总价(元):" + datalist.get(position).get("totalPrice"));
        holder.orderId.setText("订单号:" + datalist.get(position).get("bulkOrderId"));
        String imgUrl = datalist.get(position).get("mainImage");
        if (!TextUtils.isEmpty(imgUrl)) {
            if (!imgUrl.substring(0, 1).equals("h") && !imgUrl.substring(0, 1).equals("H")) {
                imgUrl = "http://" + imgUrl;
            }
        }
        Glide.with(context)
                .load(imgUrl)
                .centerCrop()
                .placeholder(R.drawable.empty_photo)
                .crossFade()
                .into(holder.image);
        return convertView;
    }

    public void update(ArrayList<HashMap<String, String>> datalist) {
        this.datalist = datalist;
        notifyDataSetChanged();
    }
}
